package com.whcoding.multithreading.thred;

/**
 * @program: spring-boot-learning
 * @description: 多线程的停止
 * @author: whcoding
 * @create: 2022-06-10 17:55
 **/
public class ThreadStop {

	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			while (true) {
				try {
					boolean interrupted = Thread.currentThread().isInterrupted();
					if (interrupted) {
						System.out.println("线程已停止");
						break;
					} else {
						System.out.println("线程执行中");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		t1.start();
		try {
			Thread.sleep(2000);
			t1.interrupt();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
